library(data.table)
library(tidyverse)


## HELPERS -----
`%notin%` <- function(x,y){!(x %in% y)}
preprocess_text <- function(x){
  x = str_replace_all(x,'\\s+',' ')
  x = tolower(x)
  return(x)
}


## What do I need?

## Which orgs signed to which briefs (amicusnet)
## Which briefs are on petitioner/respondent (amicusnet?)

source("code/acnet.R")
q <- acnet.web.query(minRange=1910, maxRange=2021)
briefs = fromJSON(q)
briefs = briefs[[1]]

briefs2 = briefs %>% 
  mutate(amicus = strsplit(as.character(organizationNames), "||", fixed=T)) %>% 
  unnest(amicus) %>%
  mutate(direction = dplyr::recode(direction, "3" = "petitioner",
                                   "1" = "respondent",
                                   "2" = "neutral"))


## Whether petitioner/respondent won (Spaeth)
## Whether winner was liberal or conservative (Spaeth)
load("data/SCDB_2021_01_caseCentered_Citation.Rdata")
scdb = SCDB_2021_01_caseCentered_Citation
rm(SCDB_2021_01_caseCentered_Citation)

scdb = scdb %>% select(partyWinning, decisionDirection, caseId) %>%
  mutate(caseID = caseId) %>%
  select(-caseId) %>%
  mutate(winner = recode(partyWinning, "0" = "respondent",
                         "1" = "petitioner",
                         "2" = "neutral"),
         outcome = recode(decisionDirection,
                          "1" = "conservative",
                          "2" = "liberal",
                          "3" = "neutral"))


## Merge
dat = briefs2 %>% inner_join(scdb, by="caseID") %>%
  select(caseID, direction, amicus, winner, outcome) %>%
  filter(direction != "neutral", outcome != "neutral") %>%
  mutate(brief_won = direction == winner) %>%
  mutate(group_ideology = ifelse((outcome=="liberal" & brief_won == TRUE)|(outcome=="conservative" & brief_won == "FALSE"),
                                 "conservative","liberal"))

dat2 = dat %>% group_by(amicus) %>%
  summarize(con = sum(group_ideology=="conservative"),
            lib = sum(group_ideology == "liberal"),
            libpct = lib/(lib+con),
            sum = con + lib) %>%
  mutate(inconsistent_pct = abs(libpct - round(libpct))) %>%
  arrange(desc(sum))

weighted.mean(dat2$inconsistent_pct,w=dat2$sum)

dat2 %>% filter(sum > 10) %>% arrange(desc(inconsistent_pct))
dat2 %>% filter(sum > 20) %>% arrange(inconsistent_pct)
